package cn.fl.institution.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstNpBscInfoService;
import cn.fl.customer.vo.CstNpBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @Description: 项目基础信息表 控制层 新邮储银行
 */
@Controller
@RequestMapping("/institution/xycbank")
public class PrjBscInfoInsXycBankController {

    private static final Logger logger = LoggerFactory.getLogger(PrjBscInfoInsXycBankController.class);

    @Autowired
    ICstNpBscInfoService cstNpBscInfoService; // 自然人信息

    /**
     * 保存 项目客户信息
     */
    @RequestMapping(value = "/xycbankSavePrjAccInfo.do", method = RequestMethod.POST)
    @ResponseBody
    public DataResultVO xycbankSavePrjAccInfo(HttpServletRequest request) throws BaseException {
        StopWatch stopWatch = new StopWatch("新邮储银行信息采集保存接口计时统计");
        stopWatch.start();
        BaseLogUtils.info(logger, "xycbankSavePrjAccInfo", "修改项目客户账户信息及校验入参"+ JSON.toJSONString(request.getParameterMap()), CurrentThreadContext.getCurrentUserName());
        String prjId = request.getParameter("prjId");//项目id
        String cstId = request.getParameter("cstId");//客户id
        String monthInc = request.getParameter("monthInc");//月收入
        DataResultVO dataResultVO = new DataResultVO();
        try {
            if (StringUtils.isEmpty(prjId)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "项目id为空!");
            }
            if (StringUtils.isEmpty(cstId)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "客户id为空!");
            }
            if(StringUtils.isEmpty(monthInc)){
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护月收入!");
            }
            CstNpBscInfoVO cstNpBscInfoVO = this.cstNpBscInfoService.selectById(Long.parseLong(cstId));
            boolean modify = false;
            if(null != cstNpBscInfoVO) {
                Long mrIncMoney = DecimalUtils.convertYuanToFen(Double.parseDouble(monthInc)); // 月收入转为分
                Double yrInc = DecimalUtils.mul(mrIncMoney.doubleValue(),12d); // 月收入转换年收入
                if (!Objects.equals(cstNpBscInfoVO.getYrInc().doubleValue(), yrInc)) {
                    cstNpBscInfoVO.setYrInc(yrInc.longValue());
                    BaseLogUtils.info(logger, "xycbankSavePrjAccInfo", "修改自然人信息cstNpBscInfoVO"+ JSON.toJSONString(cstNpBscInfoVO), CurrentThreadContext.getCurrentUserName());
                    modify = this.cstNpBscInfoService.modify(cstNpBscInfoVO);
                } else {
                    modify = true;
                }
            }
            if (modify) {
                dataResultVO.setSuccess(BaseConstant.IS_YESNO_YES);
                dataResultVO.setInfo("保存成功");
            } else {
                dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                dataResultVO.setInfo("保存失败");
            }
        } catch (BaseException ex) {
            dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            dataResultVO.setInfo(ex.getMessage());
            BaseLogUtils.info(logger, "xycbankSavePrjAccInfo", "新增或修改项目客户账户信息及校验,prjId:" + prjId + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception e) {
            BaseLogUtils.info(logger, "xycbankSavePrjAccInfo", "保存失败，系统异常",CurrentThreadContext.getCurrentUserName(), e);
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "保存失败，系统异常");
        }
        stopWatch.stop();
        BaseLogUtils.info(logger, "xycbankSavePrjAccInfo", "新增或修改项目客户账户信息及校验返回dataResultVO=" +
                JSON.toJSONString(dataResultVO) + "-耗时=" + stopWatch.prettyPrint(), CurrentThreadContext.getCurrentUserName());
        return dataResultVO;
    }

}
